import ipfilter from "express-ipfilter";
import { Request, Response, NextFunction } from "express";

export const errorHandler = (payload: Error | ipfilter.IpDeniedError, _request: Request, response: Response, _next: NextFunction) => {
	if (payload instanceof ipfilter.IpDeniedError) response.status(401).json({ data: null, message: "拒绝访问" });
	else if (payload instanceof Error) {
		console.error(payload.stack);
		response.status(response.statusCode || 400).json({
			data: response.locals.data || null,
			message: payload.message || "失败",
		});
	} else response.status(500).json({ data: null, message: "未知错误" });
};
